package com.bangmai.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import org.apache.log4j.spi.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bangmai.beans.Order2CommodityInspection;
import com.bangmai.beans.Order2Customer;
import com.bangmai.beans.Order2Immigration;
import com.bangmai.beans.Order2Payment;
import com.bangmai.beans.OrderBody2Payment;
import com.bangmai.beans.OrderGood2CommodityInspection;
import com.bangmai.beans.OrderGood2Customer;
import com.bangmai.beans.OrderGood2Immigration;
import com.bangmai.config.Config;
import com.bangmai.model.Receiver;
import com.bangmai.model.ReceiverMapper;
import com.bangmai.service.AnalysisService;
import com.bangmai.service.CustomsService;
import com.bangmai.util.FileUtil;
import com.bangmai.util.XmlUtil;

@Service
public class CustomsServiceImpl implements CustomsService {
	Logger logger = Logger.getLogger(CustomsServiceImpl.class.getName());
	@Autowired
	private XmlUtil xmlUtil;
	@Autowired
	private ReceiverMapper receiverMapper;
	@Autowired
	private AnalysisService analysisService;

	private Order2Customer order2Customer = new Order2Customer();
	private List<OrderGood2Customer> goods_custom = new ArrayList<>();

	private Order2Payment order2Payment = new Order2Payment();
	List<OrderBody2Payment> body = new ArrayList<>();

	private Order2Immigration order2Immigration = new Order2Immigration();
	List<OrderGood2Immigration> goods_Immigration = new ArrayList<>();

	@Override
	public void createAndSendPacket(int type) {
		// TODO Auto-generated method stub
		switch (type) {
		case 1:
			goods_custom.add(new OrderGood2Customer());
			xmlUtil.createCustomXml(order2Customer, goods_custom);
			break;
		case 2:
			Order2CommodityInspection order_CommodityInspection = new Order2CommodityInspection();
			List<OrderGood2CommodityInspection> goods_CommodityInspection = new ArrayList<>();
			order_CommodityInspection.MessageID = FileUtil.getTimeString1();
			order_CommodityInspection.SendTime = FileUtil.getTimeString1();
			order_CommodityInspection.EntInsideNo = FileUtil.getTimeString1();
			order_CommodityInspection.Sender = "1500003460";
			order_CommodityInspection.Receiver = "1500003460";
			order_CommodityInspection.MessageType = "661101";
			order_CommodityInspection.FunctionCode = "";
			order_CommodityInspection.Version = "1.0";
			order_CommodityInspection.Ciqbcode = "000069";
			order_CommodityInspection.CbeComcode = "1500003460";
			order_CommodityInspection.CbepComcode = "1500003460";
			order_CommodityInspection.OrderStatus = "S";
			order_CommodityInspection.ReceiveName = "巫  丹";
			order_CommodityInspection.ReceiveAddr = "四川省成都市金牛区巴蜀西环街60号青房天韵华庭2栋2单元602";
			order_CommodityInspection.ReceiveNo = "510106198201196223";
			order_CommodityInspection.ReceivePhone = "13880805616";
			order_CommodityInspection.FCY = "48.00";
			order_CommodityInspection.Fcode = "CNY";
			order_CommodityInspection.Editccode = "1500003460";
			order_CommodityInspection.DrDate = FileUtil.getTimeString1();

			OrderGood2CommodityInspection oGoods = new OrderGood2CommodityInspection();
			oGoods.EntGoodsNo = "201612";
			oGoods.Gcode = "ECO-00001";
			oGoods.Hscode = "3401110000";
			oGoods.CiqGoodsNo = "";
			oGoods.CopGName = "S&M杏味马赛皂100g/块";
			oGoods.Brand = "S&M";
			oGoods.Spec = "100g/块";
			oGoods.Origin = "法国";
			oGoods.Qty = "1";
			oGoods.QtyUnit = "017";
			oGoods.DecPrice = "48.00";
			oGoods.DecTotal = "48.00";
			oGoods.SellWebSite = "http://www.indoorbuy.com";
			oGoods.Nots = "";
			goods_CommodityInspection.add(oGoods);
			xmlUtil.createCommodityInspectionXml(order_CommodityInspection, goods_CommodityInspection);
			break;
		case 3:
			body.add(new OrderBody2Payment());
			xmlUtil.createPaymentXml(order2Payment, body);

			break;
		case 4:
			goods_Immigration.add(new OrderGood2Immigration());
			xmlUtil.createImmigrationXml(order2Immigration, goods_Immigration);
			break;
		case 5:

			break;

		default:
			break;
		}

	}

	@Override
	public void receivePacket(int type) throws Exception {
		// TODO Auto-generated method stub
		switch (type) {
		case 1:
			xmlUtil.downloadXml(Config.FTP_CUSTOM_HOST, Config.FTP_CUSTOM_PORT, Config.FTP_CUSTOM_USERNAME,
					Config.FTP_CUSTOM_PASSWORD, Config.FTP_CUSTOM_DOWNLOAD_LOCALFILEPATH,
					Config.FTP_CUSTOM_DOWNLOAD_FILEPATH, "");
			try {
				analysepacket(type);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			break;
		case 2:
			//
			xmlUtil.downloadXml(Config.FTP_CCIQ_IP, Config.FTP_CCIQ_PORT, Config.FTP_CCIQ_USERNAME,
					Config.FTP_CCIQ_PASSWORD, Config.FTP_CCIQ_DOWNLOAD_LOCALFILEPATH, Config.FTP_CCIQ_DOWNLOADPATH,
					"DOCREC_201604011853188070001_data73BC7DF5B41FCA573206DFB1BBA0E389.xml");
			analysepacket(type);
			break;
		case 3:
			break;
		case 4:
			break;
		case 5:
			break;

		default:
			break;
		}

	}

	@Override
	@Transactional
	public void analysepacket(int type) throws Exception {
		// TODO Auto-generated method stub
		switch (type) {
		case 1:
			try {
				logger.info("开始解析报文。。。");
				List<Receiver> receivers = analysisService.analysisXml(Config.FTP_CUSTOM_DOWNLOAD_LOCALFILEPATH,
						"ResponseBodyList");
				receiverMapper.save(receivers);
				receivers = null;
				
				
			} catch (Exception e) {
				// TODO: handle exception
			}finally {
				System.gc();
				logger.info("开始删除解析完毕的报文...");
				xmlUtil.removeAllFile(Config.FTP_CUSTOM_DOWNLOAD_LOCALFILEPATH);
			}

			break;
		case 2:
			List<Receiver> receivers=analysisService.analysisXml(Config.FTP_CCIQ_DOWNLOAD_LOCALFILEPATH, "Declaration");
			receiverMapper.save(receivers);
			xmlUtil.removeAllFile(Config.FTP_CCIQ_DOWNLOAD_LOCALFILEPATH);
			
			
			break;
		case 3:
			xmlUtil.parserXml(Config.PAY_LOCALFILRPATH, "MessageHead");

			break;
		case 4:

			break;
		case 5:

			break;

		default:
			break;
		}

	}

	@Override
	@Transactional
	public void update(int type, Receiver receiver) {
		// TODO Auto-generated method stub
		switch (type) {
		case 1:

			break;
		case 2:

			break;
		case 3:

			break;
		case 4:

			break;
		case 5:

			break;

		default:
			break;
		}

	}

}
